import { cdnRequest } from '@/utils/HttpRequest';

export async function findByAtterList() {
  //SwaggerResources

  return cdnRequest.get<API.BaseArrayDataResult<API.Attribute>>(
    'attr/findByList',
  );
}

export async function editAttr(params: API.Attribute) {
  return cdnRequest.post('attr/edit', params);
}
export async function editAttrValue(params: API.AttributeValue) {
  return cdnRequest.post('attr/valueEdit', params);
}

export async function findBySelectList() {
  //SwaggerResources

  return cdnRequest.get<API.BaseArrayDataResult<API.Attribute>>(
    'attr/findBySelectList',
    {
      method: 'GET',
    },
  );
}
export async function findByValueListByAttrId(attrId: number) {
  //SwaggerResources

  return cdnRequest.get<API.AttributeValue>(
    'attr/valueListByAttrId?attrId=' + attrId,
  );
}

export async function findBySelectValueList(key: string, type: number = 0) {
  //console.log('Attribute attrkey1', key);
  //SwaggerResources
  let attrkey = 'attrkey' + type + 'NUmber' + key;

  return new Promise<any[]>((resolve, reject) => {
    let items = localStorage.getItem(attrkey);
    if (items) {
      let parse = JSON.parse(items);
      //  console.log('Attribute attrkey1', parse);
      resolve(parse);
    } else {
      let url = 'attr/findBySelectValueList';
      //  console.log('Attribute attrkey', key);
      cdnRequest
        .get<any>(url, { key: key, type: type })
        .then((res) => {
          localStorage.setItem(attrkey, JSON.stringify(res));
          resolve(res);
        })
        .catch((e) => {
          reject(e);
        });
    }
  });
}
export function getResultEnum(key: string, type: number = 0) {
  // Call the getResult function with the provided key and type, and chain a .then method
  // Return the Promise that resolves to the transformed array
  return getResult(key, type).then((result) => {
    console.log('Attribute attrkey2', JSON.stringify(result));
    // Map over the result array and transform each item
    return result;
  });
}
export async function getResult(key: string, type: number = 0) {
  // console.log('Attribute attrkey2', key);
  return new Promise<API.AttributeValue[]>((resolve, reject) => {
    findBySelectValueList(key, type)
      .then((result) => {
        resolve(result);
      })
      .catch((e) => {
        reject(e);
      });
  });
}

export async function findBySelectValueListPeomise(
  columns: [any],
  type: number = 0,
) {
  for (let i = 0; i < columns.length; i++) {
    let item = columns[i];
    // eslint-disable-next-line eqeqeq
    if (item['valueType'] === 'select') {
      const valem = await getResult(item['attribute'], type);
      //  console.log(valem)
      item['valueEnum'] = valem;
      item['filters'] = true;
      item['onFilter'] = true;
      columns[i] = item;
    }
  }
  return columns;
}

export async function findBySelectValueFormListPeomise(columns: [any]) {
  for (let i = 0; i < columns.length; i++) {
    let item = columns[i];
    if (item['columns'] !== undefined) {
      //  console.log("findBySelectValueFormListPeomise")
      item['columns'] = await findBySelectValueFormListPeomise(item['columns']);
    }
    if (item['valueType'] === 'select') {
      const valem = await getResult(item['attribute'], 0);
      console.log(valem);
      item['request'] = getResult(item['attribute'], 1);
      item['valueEnum'] = valem;
      item['options'] = valem;

      columns[i] = item;
    }
  }
  return columns;
}
